package com.shxzz.easy12306.services.ticket.mq.consumer;

import com.shxzz.easy12306.framework.starter.common.mq.base.MessageWrapper;
import com.shxzz.easy12306.framework.starter.common.mq.constant.OrderRocketMQConstant;
import com.shxzz.easy12306.framework.starter.common.mq.event.PayResultCallbackOrderEvent;
import com.shxzz.easy12306.framework.starter.idempotent.annotation.Idempotent;
import com.shxzz.easy12306.framework.starter.idempotent.enums.IdempotentKeyType;
import com.shxzz.easy12306.framework.starter.idempotent.enums.IdempotentScene;
import com.shxzz.easy12306.services.ticket.common.constant.RocketMQConstant;
import com.shxzz.easy12306.services.ticket.service.SeatService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * @author shxzz
 * @create 2024-03-17-20:25
 */
@Slf4j
@RocketMQMessageListener(
        topic = OrderRocketMQConstant.PAY_CALLBACK_ORDER_TOPIC,
        selectorExpression = OrderRocketMQConstant.PAY_CALLBACK_ORDER_TAG,
        consumerGroup = RocketMQConstant.PAY_CALLBACK_TICKET_CG
)
@RequiredArgsConstructor
@Component
public class PayCallbackConsumer implements RocketMQListener<MessageWrapper<PayResultCallbackOrderEvent>> {

    private final SeatService seatService;

    @Idempotent(
            scene = IdempotentScene.MQ,
            type = IdempotentKeyType.SPEL,
            uniqueKeyPrefix = "easy12306-ticket:pay-callback:",
            spel = "#message.getKeys()",
            keyTimout = 7200L
    )
    @Override
    public void onMessage(MessageWrapper<PayResultCallbackOrderEvent> message) {
        PayResultCallbackOrderEvent payResultCallbackOrderEvent = message.getMessage();
        log.info("消费,支付回调，订单号：{}", payResultCallbackOrderEvent.getOrderSn());
        seatService.payCallback(payResultCallbackOrderEvent.getOrderSn());
    }
}
